<?php
    namespace dg\Controller;

    use Think\Controller;

    header("Content-type: text/html; charset=utf-8");

    class IndexController extends ApibaseController
    {
        /*
         * 护士用户列表
         */
        public function nurse()
        {
            $page = I('get.page', 1);
            $perpage = 10;
            $url = [];
            $list = M('NurseEntity');
            if (I('get.cust_name')) {
                $list = $list->where('uname LIKE "%' . I('get.cust_name') . '%"');
                $url['cust_name'] = I('get.cust_name');
            }
            if (I('get.cust_mobile')) {
                $list = $list->where(['cust_mobile' => I('get.cust_mobile')]);
                $url['cust_mobile'] = I('get.cust_mobile');
            }

            $custObject = clone $list;
            $getData = $list->order(['add_at' => 'desc'])->page($page, $perpage)->select();
            $this->assign('page', setPage($custObject->count(), $perpage, $page, '?' . http_build_query($url)));
            $this->assign('getData', $getData);
            $this->display('index/nurse');
        }

        /**
         * 消券管理
         */
        public function input()
        {
            $page = I('get.page', 1);
            $perpage = 10;
            $xq_stime = I('get.xq_stime');
            $xq_etime = I('get.xq_etime');
            $url = [];
            $list = M('InputInfo io')
                    ->join('qxww_card_info ci ON io.card_number = ci.card_number')
                    ->field('io.*,ci.card_status')
                    ->group('io.card_number')
                    ->where(['io.status' => 0]);

            if (trim(I('get.card_number'))) {
                $list = $list->where(['io.card_number' => trim(I('get.card_number'))]);
                $url['card_number'] = trim(I('get.card_number'));
            }
            if (trim(I('get.user_name'))) {
                $list = $list->where(['io.user_name' => trim(I('get.user_name'))]);
                $url['user_name'] = trim(I('get.user_name'));
            }
            if (trim(I('get.user_mobile'))) {
                $list = $list->where(['io.user_mobile' => trim(I('get.user_mobile'))]);
                $url['user_mobile'] = trim(I('get.user_mobile'));
            }
            if (in_array(I('get.quan_type'),[1,3])) {
                $list = $list->where(['ci.card_status' => I('get.quan_type')]);
                $url['quan_type'] = I('get.quan_type');
            }

            //销券时间筛选
            if ($xq_stime && !$xq_etime) {
                $list = $list->where(['io.add_at' => ['gt', strtotime($xq_stime)]]);
                $url['xq_stime'] = $xq_stime;
            } elseif (!$xq_stime && $xq_etime) {
                $list = $list->where(['io.add_at' => ['lt', strtotime($xq_etime)]]);
                $url['xq_etime'] = $xq_etime;
            } elseif ($xq_stime && $xq_etime) {
                $list = $list->where(['io.add_at' => ['between', [strtotime($xq_stime), strtotime($xq_etime) + 86399]]]);
                $url['xq_stime'] = $xq_stime;
                $url['xq_etime'] = $xq_etime;
            }
            $custObject = clone $list;
            $getData = $list->order(['io.add_at' => 'desc'])->page($page, $perpage)->select();

            $this->assign('page', setPage(count($getData), $perpage, $page, '?' . http_build_query($url)));
            $this->assign('getData', $getData);
//            if ($role_id == 6) {
//                $this->display('index/nurse_input');
//            }
            $this->display('index/cust_input');
        }

        /**
         * 修补管理
         */
        public function repair()
        {
            $page = I('get.page', 1);
            $perpage = 10;
            $xb_stime = I('get.xb_stime');
            $xb_etime = I('get.xb_etime');

            $url = [];
            $list = M('InputInfo')->where(['quan_type' => 2,'status'=>0]);

            if (trim(I('get.card_number'))) {
                $list = $list->where(['card_number' => trim(I('get.card_number'))]);
                $url['card_number'] = trim(I('get.card_number'));
            }
            if (trim(I('get.user_name'))) {
                $list = $list->where(['user_name' => trim(I('get.user_name'))]);
                $url['user_name'] = trim(I('get.user_name'));
            }

            if (trim(I('get.user_mobile'))) {
                $list = $list->where(['user_mobile' => trim(I('get.user_mobile'))]);
                $url['user_mobile'] = trim(I('get.user_mobile'));
            }
            if (trim(I('get.xiubu_uname'))) {
                $list = $list->where(['xiubu_uname' => trim(I('get.xiubu_uname'))]);
                $url['xiubu_uname'] = trim(I('get.xiubu_uname'));
            }
            if (trim(I('get.xiubu_mobile'))) {
                $list = $list->where(['xiubu_mobile' => trim(I('get.xiubu_mobile'))]);
                $url['xiubu_mobile'] = trim(I('get.xiubu_mobile'));
            }
            if (in_array(I('get.xiubu_type'),[1,2])) {
                $list = $list->where(['xiubu_type' => I('get.xiubu_type')]);
                $url['xiubu_type'] = I('get.xiubu_type');
            }

            $custObject = clone $list;
            $getData = $list->order(['add_at' => 'desc'])->page($page, $perpage)->select();
//            echo $list->_sql();exit;
            $this->assign('page', setPage($custObject->count(), $perpage, $page, '?' . http_build_query($url)));
            $this->assign('getData', $getData);
            $this->display('index/repair');
        }




        /**
         * 消券管理 导出数据
         */
        public function inputExportExcel()
        {
            set_time_limit(0);
            $xq_stime = I('get.xq_stime');
            $xq_etime = I('get.xq_etime');
            $url = [];
            $list = M('InputInfo io')
                ->join('qxww_card_info ci ON io.card_number = ci.card_number')
                ->field('io.*,ci.card_status')
                ->group('io.card_number')
                ->where(['io.status' => 0]);

            if (trim(I('get.card_number'))) {
                $list = $list->where(['io.card_number' => trim(I('get.card_number'))]);
                $url['card_number'] = trim(I('get.card_number'));
            }
            if (trim(I('get.user_name'))) {
                $list = $list->where(['io.user_name' => trim(I('get.user_name'))]);
                $url['user_name'] = trim(I('get.user_name'));
            }
            if (trim(I('get.user_mobile'))) {
                $list = $list->where(['io.user_mobile' => trim(I('get.user_mobile'))]);
                $url['user_mobile'] = trim(I('get.user_mobile'));
            }
            if (in_array(I('get.quan_type'),[1,3])) {
                $list = $list->where(['ci.card_status' => I('get.quan_type')]);
                $url['quan_type'] = I('get.quan_type');
            }

            //销券时间筛选
            if ($xq_stime && !$xq_etime) {
                $list = $list->where(['io.add_at' => ['gt', strtotime($xq_stime)]]);
                $url['xq_stime'] = $xq_stime;
            } elseif (!$xq_stime && $xq_etime) {
                $list = $list->where(['io.add_at' => ['lt', strtotime($xq_etime)]]);
                $url['xq_etime'] = $xq_etime;
            } elseif ($xq_stime && $xq_etime) {
                $list = $list->where(['io.add_at' => ['between', [strtotime($xq_stime), strtotime($xq_etime) + 86399]]]);
                $url['xq_stime'] = $xq_stime;
                $url['xq_etime'] = $xq_etime;
            }
             $tableHeader = array(
                 '卡券编号','销券人','顾客手机号','顾客','销售类型','卡券状态','店家','服务商','课区','课长','医生','仪器护士','配台护士','咨询师','直接咨询师','间接咨询师','设计师','高级设计师','操作项目','销券时间'
              );
            $cardMsg = $list->order(['io.add_at' => 'desc'])->page($page, $perpage)->select();

            $salerType = ['其他','一次性10张','一次性50张','一次性500张'];
            if ($cardMsg) {
                foreach ($cardMsg as $k => $v) {
                    $data[] = [
                        '0' => $v['card_number'],
                        '1' => $v['input_name'],
                        '2' => $v['user_mobile'],
                        '3' => $v['user_name'],
                        '4' => $salerType[$v['sales_type']],
                        '5' => $v['card_status']==3?'已修补':'已销券未修补',
                        '6' => $v['shoper'],
                        '7' => $v['servicer'],
                        '8' => $v['class_area'],
                        '9' => $v['director'],
                        '10' => $v['doctor'],
                        '11' => $v['instrument_nurse'],
                        '12' => $v['matching_nurse'],
                        '13' => $v['counselor'],
                        '14' => $v['direct_counselor'],
                        '15' => $v['indirect_counselor'],
                        '16' => $v['designer'],
                        '17' => $v['senior_designer'],
                        '18' => $v['use_project'],
                        '19' => date('Y-m-d',$v['add_at'])
                    ];
                }
            }
            if($data){
                exportExcel($tableHeader,$data);
            }else{
                $this->showtips('没有数据');
            }

        }

        /**
         * 修补管理 导出数据
         */
        public function repairExportExcel()
        {
            set_time_limit(0);
            $list = M('InputInfo')->where(['quan_type' => 2,'status'=>0]);
            if (trim(I('get.card_number'))) {
                $list = $list->where(['card_number' => trim(I('get.card_number'))]);
                $url['card_number'] = trim(I('get.card_number'));
            }
            if (trim(I('get.user_name'))) {
                $list = $list->where(['user_name' => trim(I('get.user_name'))]);
                $url['user_name'] = trim(I('get.user_name'));
            }

            if (trim(I('get.user_mobile'))) {
                $list = $list->where(['user_mobile' => trim(I('get.user_mobile'))]);
                $url['user_mobile'] = trim(I('get.user_mobile'));
            }
            if (in_array(I('get.xiubu_type'),[1,2])) {
                $list = $list->where(['xiubu_type' => I('get.xiubu_type')]);
                $url['xiubu_type'] = I('get.xiubu_type');
            }
            $tableHeader = array(
                '卡券编号','销券人','修补类型','修补金额','修补人手机','修补人姓名','销售类型','店家','服务商','课区','课长','医生','仪器护士','配台护士','咨询师','直接咨询师','间接咨询师','设计师','高级设计师','修补项目'
            );
            $cardMsg = $list->order(['add_at' => 'desc'])->select();

            $salerType = ['其他','一次性10张','一次性50张','一次性500张'];
            if ($cardMsg) {
                foreach ($cardMsg as $k => $v) {
                    $data[] = [
                        '0' => $v['card_number'],
                        '1' => $v['input_name'],
                        '2' => $v['xiubu_type']==1?'本人修补':'他人修补',
                        '3' => $v['xiubu_money'],
                        '4' => $v['user_mobile'],
                        '5' => $v['user_name'],
                        '6' => $salerType[$v['sales_type']],
                        '7' => $v['shoper'],
                        '8' => $v['servicer'],
                        '9' => $v['class_area'],
                        '10' => $v['director'],
                        '11' => $v['doctor'],
                        '12' => $v['instrument_nurse'],
                        '13' => $v['matching_nurse'],
                        '14' => $v['counselor'],
                        '15' => $v['direct_counselor'],
                        '16' => $v['indirect_counselor'],
                        '17' => $v['designer'],
                        '18' => $v['senior_designer'],
                        '19' => $v['use_project']
                    ];
                }
            }
            if($data){
                exportExcel($tableHeader,$data);
            }else{
                $this->showtips('没有数据');
            }

        }

        /**
         * 添加/修改 录入信息
         */
        public function addInput()
        {
            if (I('post.dosubmit')) {
                $data = [
//                    'user_name' => trim(I('post.user_name')),
//                    'user_mobile' => trim(I('post.user_mobile')),
                    'shoper' => trim(I('post.shoper')),
                    'servicer' => trim(I('post.servicer')),
                    'class_area' => trim(I('post.class_area')),
                    'director' => trim(I('post.director')),
                    'doctor' => trim(I('post.doctor')),
                    'counselor' => trim(I('post.counselor')),
                    'direct_counselor' => trim(I('post.direct_counselor')),
                    'indirect_counselor' => trim(I('post.indirect_counselor')),
                    'designer' => trim(I('post.designer')),
                    'senior_designer' => trim(I('post.senior_designer')),
                    'instrument_nurse' => trim(I('post.instrument_nurse')),
                    'matching_nurse' => trim(I('post.matching_nurse')),
                    'xiubu_money' => trim(I('post.xiubu_money')),
                    'sales_type' => trim(I('post.sales_type')),
                    'update_at' => time()
                ];
                if ($data) {
                    if (I('post.id')) {
                        M('InputInfo')->where(['id' => I('post.id')])->setField($data);
                        putlog('录入信息修改 `' . I('post.id') . '`', 'IndexController', 'addInput');
                        $this->showtips('更新成功');
                    }
                } else {
                    $this->showtips('操作失败');
                }
            } elseif (I('get.id')) {
                $getData = M('InputInfo')->where(['id' => I('get.id')])->find();
                $this->assign('getData', $getData);
                //                if ($role_id == 6) {
                //                    $this->display('index/add_nurse_input');
                //                }
            }
                //            if ($role_id == 6) {
                //                $this->display('index/add_nurse_input');
                //            }
            $this->display('index/add_input');
        }


        /**
         * 添加/修改 修补信息
         */
        public function addRepair()
        {
            if (I('post.dosubmit')) {
                $data = [
                    'shoper'            => trim(I('post.shoper')),
                    'servicer'          => trim(I('post.servicer')),
                    'class_area'        => trim(I('post.class_area')),
                    'director'          => trim(I('post.director')),
                    'doctor'            => trim(I('post.doctor')),
                    'counselor'         => trim(I('post.counselor')),
                    'direct_counselor'  => trim(I('post.direct_counselor')),
                    'indirect_counselor'=> trim(I('post.indirect_counselor')),
                    'designer'          => trim(I('post.designer')),
                    'senior_designer'   => trim(I('post.senior_designer')),
                    'instrument_nurse'  => trim(I('post.instrument_nurse')),
                    'matching_nurse'    => trim(I('post.matching_nurse')),
                    'xiubu_money'       => trim(I('post.xiubu_money')),
                    'sales_type'        => trim(I('post.sales_type')),
                    'update_at'         => time()
                ];
                if ($data) {
                    if (I('post.id')) {
                        $add = M('InputInfo')->where(['id' => I('post.id')])->setField($data);
                        putlog('录入信息修改 `' . I('post.id') . '`', 'IndexController', 'addInput');
                        $this->showtips('更新成功');
                    }
                } else {
                    $this->showtips('操作失败');
                }
            }
        }

        /**
         * 删除录入信息
         */
        public function delInput()
        {
            if (I('get.id')) {
                $res = M('InputInfo')->where(['id' => I('get.id')])->setField(['status' => 1, 'update_at' => time()]);
                if ($res) {
                    $this->showtips('删除成功');
                } else {
                    $this->showtips('删除失败');
                }
            }
        }



        /*
         * 用户编辑
         */
        public function addcust()
        {
            if (I('post.dosubmit')) {
                $data = ['cust_realname' => I('post.cust_realname'), 'hy' => I('post.hy'), 'job' => I('post.job'),];
                if ($data) {
                    if (I('post.cust_id')) {
                        M('CustEntity')->where(['cust_id' => I('post.cust_id')])->setField($data);
                        putlog('用户信息修改 `' . I('post.cust_realname') . '`', 'IndexController', 'addcust');
                        $this->showtips('更新成功');
                    } else {
                        M('CustEntity')->add(array_merge($data, ['add_at' => time()]));
                        putlog('用户信息添加 `' . I('post.cust_realname') . '`', 'IndexController', 'addcust');
                        $this->showtips('用户添加成功');
                    }
                } else {
                    $this->showtips('操作失败');
                }
            } elseif (I('get.cust_id')) {
                $custData = M('CustEntity')->where(['cust_id' => I('get.cust_id')])->find();
                $this->assign('custData', $custData);
                $this->display('index/addcust');
            }

            $this->display('index/addcust');
        }

        /*
         * 冻结&解封用户
         */
        public function decate()
        {
            if (I('get.types')) {
                M('NurseEntity')->where(['id' => I('get.custid')])->setField(['checked' => '1']);
                $this->showtips('成功解封该用户');
            }
            M('NurseEntity')->where(['id' => I('get.custid')])->setField(['checked' => '0']);
            $this->showtips('成功冻结该用户');
        }




        /**
         * 卡券管理
         */
        public function card()
        {
            $page = I('get.page', 1);
            $perpage = I('get.perpage', 10);
            $url = [];
            $list = M('CardInfo');
            if (trim(I('get.card_number'))) {
                $list = $list->where(['card_number' => trim(I('get.card_number'))]);
                $url['card_number'] = trim(I('get.card_number'));
            }
            if (in_array(I('get.card_status'), ['1', '2', '4'])) {
                $list = $list->where(['card_status' => (I('get.card_status') - 1)]);
                $url['card_status'] = I('get.card_status');
            }
            if (in_array(I('get.xiubu_type'), ['1', '2'])) {
                $list = $list->where(['xiubu_type' => (I('get.xiubu_type') - 1)]);
                $url['xiubu_type'] = I('get.xiubu_type');
            }
            if (trim(I('get.user_name'))) {
                $list = $list->where(['user_name' => trim(I('get.user_name'))]);
                $url['user_name'] = trim(I('get.user_name'));
            }
            if (trim(I('get.use_project'))) {
                $list = $list->where('use_project like "%' . trim(I('get.use_project')) . '%"');
                $url['use_project'] = trim(I('get.use_project'));
            }
            if (trim(I('get.xiubu_project'))) {
                $list = $list->where('xiubu_project like "%' . trim(I('get.xiubu_project')) . '%"');
                $url['xiubu_project'] = trim(I('get.xiubu_project'));
            }
            if (trim(I('get.user_mobile'))) {
                $list = $list->where(['user_mobile' => trim(I('get.user_mobile'))]);
                $url['user_mobile'] = trim(I('get.user_mobile'));
            }
            if (trim(I('get.group_place'))) {
                $list = $list->where(['group_place' => trim(I('get.group_place'))]);
                $url['group_place'] = trim(I('get.group_place'));
            }
            $copy = clone $list;
            $getData = $list->select();
            $card_status = ['1' => '可用', '2' => '已使用未修补', '4' => '已修补'];
            $this->assign('page', setPage($copy->count(), $perpage, $page, '?' . http_build_query($url)));
            $this->assign('getData', $getData);
            $this->assign('card_status', $card_status);
            $this->display('index/card');
        }

        /**
         * 查看修补详情
         */
        public function getXiubuInfo()
        {
            if(I('get.id')){
                $getData = M('InputInfo')->where(['id'=>I('get.id')])->find();
                $this->assign('getData',$getData);
                $this->display('index/xiubu_info');
            }
        }

        /**
         * 销券管理 导入数据
         */
        public function xiaoquanImport()
        {
            if ($_FILES) {
                //定义允许上传的文件扩展名
                $ext_arr   = ['xls', 'xlsx',];
                $file_name = $_FILES['file']['name'];
                //获得文件扩展名
                $temp_arr = explode(".", $file_name);
                $file_ext = array_pop($temp_arr);
                $file_ext = trim($file_ext);
                $file_ext = strtolower($file_ext);
                if (!in_array($file_ext, $ext_arr)) {
                    dataFormat('', 2, '文件类型错误');
                }
                if ($_FILES['file']['error']) {
                    switch ($_FILES['file']['error']) {
                        case '1':
                            $error = '超过php.ini允许的大小。';
                            break;
                        case '2':
                            $error = '超过表单允许的大小。';
                            break;
                        case '3':
                            $error = '图片只有部分被上传。';
                            break;
                        case '4':
                            $error = '请选择图片。';
                            break;
                        case '6':
                            $error = '找不到临时目录。';
                            break;
                        case '7':
                            $error = '写文件到硬盘出错。';
                            break;
                        case '8':
                            $error = 'File upload stopped by extension。';
                            break;
                        default:
                            $error = '未知错误。';
                    }
                    dataFormat('', 1, $error);
                }
                $url = $_FILES['file']['tmp_name'];
                $res = importExcel($url);
                if ($res) {
                    $InputInfo = M('InputInfo');
                    $InputInfo->startTrans();
                    $maxSign = $InputInfo->max('import_sign');
                    $maxSign = $maxSign?:0;
                    foreach ($res as $k1 => $v1) {
                        //券号没填写的行 默认空行  删除  券号不正确的删除
                        if (empty($v1[0])) {
                            unset($res[ $k1 ]);
                            continue;
                        }
                        $ri = $InputInfo->where(['card_number'=>$v1[0],'quan_type'=>1])->count();
                        if(!$ri){
                            unset($res[ $k1 ]);
                            continue;
                        }
                    }
                    $count = count($res);
                    if ($count == 0) {
                        dataFormat('', 4, '没有数据，请导入正确的表格');
                    }
                    $succ     = 0;
                    foreach ($res as $k => $v) {
                        $sales_type = 0;
                        if($v[1]){
                            preg_match_all('/\d{2,3}/',$v[1],$r);
                            $numb = $r[0][0];
                            if($numb == '10'){
                                $sales_type = 1;
                            }elseif($numb == '50'){
                                $sales_type = 2;
                            }elseif($numb == '500'){
                                $sales_type = 3;
                            }
                        }

                        $setData = [];
                        if(!empty(trim($v[2]))){
                            $setData['counselor'] = trim($v[2]);
                        }
                        if(!empty(trim($v[3]))){
                            $setData['direct_counselor'] = trim($v[3]);
                        }
                        if(!empty(trim($v[4]))){
                            $setData['indirect_counselor'] = trim($v[4]);
                        }
                        if(!empty(trim($v[5]))){
                            $setData['designer'] = trim($v[5]);
                        }
                        if(!empty(trim($v[6]))){
                            $setData['senior_designer'] = trim($v[6]);
                        }
                        if(!empty(trim($v[7]))){
                            $setData['shoper'] = trim($v[7]);
                        }
                        if(!empty(trim($v[8]))){
                            $setData['servicer'] = trim($v[8]);
                        }
                        if(!empty(trim($v[9]))){
                            $setData['class_area'] = trim($v[9]);
                        }
                        if(!empty(trim($v[10]))){
                            $setData['director'] = trim($v[10]);
                        }
                        if(!empty(trim($v[11]))){
                            $setData['doctor'] = trim($v[11]);
                        }
                        if(!empty(trim($v[12]))){
                            $setData['instrument_nurse'] = trim($v[12]);
                        }
                        if(!empty(trim($v[13]))){
                            $setData['matching_nurse'] = trim($v[13]);
                        }

                        $setData['update_at'] = time();
                        $setData['import_at'] = time();
                        $setData['sales_type'] = $sales_type;
                        $setData['import_sign'] = $maxSign+1;
                        $set = $InputInfo
                            ->lock(true)
                            ->where(['card_number'=>trim($v[0]),'quan_type'=>1])
                            ->setField($setData);
                        if ($set) {
                            $succ += 1;
                        }
                    }
                    if ($succ == $count) {
                        $InputInfo->commit();
                        dataFormat('', 0, $maxSign+1);
                    } else {
                        $InputInfo->rollback();
                        dataFormat('', 5, '导入失败[1]');
                    }
                } else {
                    dataFormat('', 3, '没有数据，导入失败[0]');
                }
            }
        }

        /**
         * 修补管理 导入数据
         */
        public function repairImport()
        {
            if ($_FILES) {
                //定义允许上传的文件扩展名
                $ext_arr   = ['xls', 'xlsx',];
                $file_name = $_FILES['file']['name'];
                //获得文件扩展名
                $temp_arr = explode(".", $file_name);
                $file_ext = array_pop($temp_arr);
                $file_ext = trim($file_ext);
                $file_ext = strtolower($file_ext);
                if (!in_array($file_ext, $ext_arr)) {
                    dataFormat('', 2, '文件类型错误');
                }
                if ($_FILES['file']['error']) {
                    switch ($_FILES['file']['error']) {
                        case '1':
                            $error = '超过php.ini允许的大小。';
                            break;
                        case '2':
                            $error = '超过表单允许的大小。';
                            break;
                        case '3':
                            $error = '图片只有部分被上传。';
                            break;
                        case '4':
                            $error = '请选择图片。';
                            break;
                        case '6':
                            $error = '找不到临时目录。';
                            break;
                        case '7':
                            $error = '写文件到硬盘出错。';
                            break;
                        case '8':
                            $error = 'File upload stopped by extension。';
                            break;
                        default:
                            $error = '未知错误。';
                    }
                    dataFormat('', 1, $error);
                }
                $url = $_FILES['file']['tmp_name'];
                $res = importExcel($url);
                if ($res) {
                    $InputInfo = M('InputInfo');
                    $InputInfo->startTrans();
                    $maxSign = $InputInfo->max('import_sign');
                    $maxSign = $maxSign?:0;
                    foreach ($res as $k1 => $v1) {
                        //券号没填写的行 默认空行  删除
                        if (empty($v1[0])) {
                            unset($res[ $k1 ]);
                            continue;
                        }
                        $ri = $InputInfo->where(['card_number'=>$v1[0],'quan_type'=>2])->count();
                        if(!$ri){
                            unset($res[ $k1 ]);
                            continue;
                        }
                    }
                    $count = count($res);
                    if ($count == 0) {
                        dataFormat('', 4, '没有数据，请导入正确的表格');
                    }
                    $succ     = 0;
                    foreach ($res as $k => $v) {
                        $sales_type = 0;
                        if($v[1]){
                            preg_match_all('/\d{2,3}/',$v[1],$r);
                            $numb = $r[0][0];
                            if($numb == '10'){
                                $sales_type = 1;
                            }elseif($numb == '50'){
                                $sales_type = 2;
                            }elseif($numb == '500'){
                                $sales_type = 3;
                            }
                        }

                        $setData = [];
                        if(trim($v[2])){
                            $setData['counselor'] = trim($v[2]);
                        }
                        if(trim($v[3])){
                            $setData['direct_counselor'] = trim($v[3]);
                        }
                        if(trim($v[4])){
                            $setData['indirect_counselor'] = trim($v[4]);
                        }
                        if(trim($v[5])){
                            $setData['designer'] = trim($v[5]);
                        }
                        if(trim($v[6])){
                            $setData['senior_designer'] = trim($v[6]);
                        }
                        if(trim($v[7])){
                            $setData['shoper'] = trim($v[7]);
                        }
                        if(trim($v[8])){
                            $setData['servicer'] = trim($v[8]);
                        }
                        if(trim($v[9])){
                            $setData['class_area'] = trim($v[9]);
                        }
                        if(trim($v[10])){
                            $setData['director'] = trim($v[10]);
                        }
                        if(trim($v[11])){
                            $setData['doctor'] = trim($v[11]);
                        }
                        if(trim($v[12])){
                            $setData['instrument_nurse'] = trim($v[12]);
                        }
                        if(trim($v[13])){
                            $setData['matching_nurse'] = trim($v[13]);
                        }
                        if(trim($v[14])){
                            $setData['xiubu_money'] = trim($v[14]);
                        }
                        $setData['update_at'] = time();
                        $setData['import_at'] = time();
                        $setData['sales_type'] = $sales_type;
                        $setData['import_sign'] = $maxSign+1;
                        $set = $InputInfo
                            ->lock(true)
                            ->where(['card_number'=>trim($v[0]),'quan_type'=>2])
                            ->setField($setData);
                        if ($set) {
                            $succ += 1;
                        }
                    }
                    if ($succ == $count) {
                        $InputInfo->commit();
                        dataFormat('', 0, $maxSign+1);
                    } else {
                        $InputInfo->rollback();
                        dataFormat('', 5, '导入失败[1]');
                    }
                } else {
                    dataFormat('', 3, '导入失败[0]');
                }
            }
        }

        /**
         * 获取消券管理导入之后的数据
         */
        public function getInputImport()
        {
            $page = I('get.page', 1);
            $perpage = 10;
            $xq_stime = I('get.xq_stime');
            $xq_etime = I('get.xq_etime');
            $sign = I('get.sign');
            $url = [];
            $url['sign'] = $sign;
            $list = M('InputInfo io')
                ->join('qxww_card_info ci ON io.card_number = ci.card_number')
                ->field('io.*,ci.card_status')
                ->group('io.card_number')
                ->where(['io.status' => 0,'io.import_sign'=>intval($sign)]);

            if (trim(I('get.card_number'))) {
                $list = $list->where(['io.card_number' => trim(I('get.card_number'))]);
                $url['card_number'] = trim(I('get.card_number'));
            }
            if (trim(I('get.user_name'))) {
                $list = $list->where(['io.user_name' => trim(I('get.user_name'))]);
                $url['user_name'] = trim(I('get.user_name'));
            }
            if (trim(I('get.user_mobile'))) {
                $list = $list->where(['io.user_mobile' => trim(I('get.user_mobile'))]);
                $url['user_mobile'] = trim(I('get.user_mobile'));
            }
            if (in_array(I('get.quan_type'),[1,3])) {
                $list = $list->where(['ci.card_status' => I('get.quan_type')]);
                $url['quan_type'] = I('get.quan_type');
            }

            
            //销券时间筛选
            if ($xq_stime && !$xq_etime) {
                $list = $list->where(['io.add_at' => ['gt', strtotime($xq_stime)]]);
                $url['xq_stime'] = $xq_stime;
            } elseif (!$xq_stime && $xq_etime) {
                $list = $list->where(['io.add_at' => ['lt', strtotime($xq_etime)]]);
                $url['xq_etime'] = $xq_etime;
            } elseif ($xq_stime && $xq_etime) {
                $list = $list->where(['io.add_at' => ['between', [strtotime($xq_stime), strtotime($xq_etime) + 86399]]]);
                $url['xq_stime'] = $xq_stime;
                $url['xq_etime'] = $xq_etime;
            }

            $getData = $list->order(['io.add_at' => 'desc'])->page($page, $perpage)->select();

            $this->assign('page', setPage(M('InputInfo')->where(['import_sign'=>$sign])->count(), $perpage, $page, '?' . http_build_query($url)));
            $this->assign('getData', $getData);
            $this->display('index/input_import');
        }

        /**
         * 获取修补管理 导入之后的数据
         * create 12-22
         * liuqi
         */
        public function getRepairImport()
        {
            $page = I('get.page', 1);
            $perpage = 10;
            $xb_stime = I('get.xb_stime');
            $xb_etime = I('get.xb_etime');
            $sign = I('get.sign');
            $url = [];
            $url['sign'] = $sign;
            $list = M('InputInfo')->where(['quan_type' => 2,'status'=>0]);

            if (trim(I('get.card_number'))) {
                $list = $list->where(['card_number' => trim(I('get.card_number'))]);
                $url['card_number'] = trim(I('get.card_number'));
            }
            if (trim(I('get.user_name'))) {
                $list = $list->where(['user_name' => trim(I('get.user_name'))]);
                $url['user_name'] = trim(I('get.user_name'));
            }

            if (trim(I('get.user_mobile'))) {
                $list = $list->where(['user_mobile' => trim(I('get.user_mobile'))]);
                $url['user_mobile'] = trim(I('get.user_mobile'));
            }
            if (trim(I('get.xiubu_uname'))) {
                $list = $list->where(['xiubu_uname' => trim(I('get.xiubu_uname'))]);
                $url['xiubu_uname'] = trim(I('get.xiubu_uname'));
            }
            if (trim(I('get.xiubu_mobile'))) {
                $list = $list->where(['xiubu_mobile' => trim(I('get.xiubu_mobile'))]);
                $url['xiubu_mobile'] = trim(I('get.xiubu_mobile'));
            }
            if (in_array(I('get.xiubu_type'),[1,2])) {
                $list = $list->where(['xiubu_type' => I('get.xiubu_type')]);
                $url['xiubu_type'] = I('get.xiubu_type');
            }

            $custObject = clone $list;
            $getData = $list->order(['add_at' => 'desc'])->page($page, $perpage)->select();
//            echo $list->_sql();exit;
            $this->assign('page', setPage($custObject->count(), $perpage, $page, '?' . http_build_query($url)));
            $this->assign('getData', $getData);
            $this->display('index/repair_import');
        }


    }